package kr.ac.hufs;

import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.StringTokenizer;

public class sockRelatedFunc {
	public final static char ftpseparator = '/';

	public int getServerResponse(StringBuffer DATA_PORT_NUM, Socket socket,
			TextArea textArea) throws IOException {

		String buf = null;
		String port1;
		int i_port1;
		String port2;
		int i_port2;
		StringBuffer tmp = new StringBuffer();
		String port3;		
		InputStream is = socket.getInputStream();
		DataInputStream in = new DataInputStream(is);
		String[] arr = new String[6];

		for (;;) {
			buf = null;
			buf = in.readLine();
			textArea.append("Response Message : " + buf + "\n");
			String buf2 = null;
			System.out.println(buf);
			buf2 = buf;

			boolean b = buf.startsWith("227");
			if (b == true) {
				DATA_PORT_NUM.delete(0, 100);
				arr = buf.split(",");
				port1 = arr[4].toString();
				port2 = arr[5].toString();
				if (port2.length() == 4) {
					tmp.append(port2.charAt(0));
					tmp.append(port2.charAt(1));
					tmp.append(port2.charAt(2));
				} else if (port2.length() == 3) {
					tmp.append(port2.charAt(0));
					tmp.append(port2.charAt(1));
				}else if (port2.length() == 5){
				tmp.append(port2.charAt(0));
				tmp.append(port2.charAt(1));
				tmp.append(port2.charAt(2));
			}
				port3 = tmp.toString();
				i_port1 = Integer.parseInt(port1);
				i_port2 = Integer.parseInt(port3);
				var.I_DATA_PORT_NUM = (i_port1 * 256) + i_port2;
				System.out.println("PORT : "+var.I_DATA_PORT_NUM);
				DATA_PORT_NUM.append(var.I_DATA_PORT_NUM);
			}
			if (buf2.charAt(0) == '5') {
				return 5;
			} else if (buf2.charAt(0) == '4') {
				return 4;
			} else if (buf2.charAt(0) == '3') {
				return 3;
			} else if (buf2.charAt(0) == '2') {
				return 2;
			} else if (buf2.charAt(0) == '1') {
				return 1;
			}

		}

	}

	public void getServerFTPData(Socket socket, StringBuffer PATHNAME,
			int requestN,UserInfoProvider uip) throws IOException {
			FileTransferManager ftf =new FileTransferManager();

		InputStreamReader stream = new InputStreamReader(
				socket.getInputStream(), "UTF-8");
		BufferedReader reader = new BufferedReader(stream);

		// InputStream is = socket.getInputStream();
		// DataInputStream in = new DataInputStream(is);
		int i =0 , a=0, b=0 ,c =0,d=0;
		String file_nlist[] = new String[1024];
		StringBuffer tmp = new StringBuffer();
		StringBuffer size =new StringBuffer();
		//String directory[] =new String[1024];
		//String file[] =new String[1024];
		String buf = null;
		switch (requestN) {
		case var.LS:
			//buf = reader.readLine();
			//System.out.println(buf);
			while ((buf = reader.readLine())!= null) {										
				 file_nlist[i]=buf;
				 System.err.println(buf);
				 for(a=49 ; a<buf.length() ; a++)
				 {					
					tmp.append(buf.charAt(a));
				 }
				 a=0;					
				 
				 if(buf.charAt(0)=='d')
				{					
					 uip.directory[b]= tmp.toString();
					 b++;	
					
				}
				 else {
					
					 uip.file[c]=tmp.toString();
					 c++;					 
				 }
				
				 //System.out.println(directory[i]);
				 tmp.delete(0, tmp.length());
				 i++;
			}						
			
			buf = null;			
			break;	
		case var.TYPE:
			buf = reader.readLine();
			System.out.println(buf);
			break;
		case var.GET:
			//getFile(socket, uip);
			ftf.strFile(socket,uip,var.BINARY_MODE);
			break;
		case var.PUT:
			ftf.sendFile(socket, uip,var.BINARY_MODE);
			
			break;
		}

	}


}
